<?php
if(isset($_POST['action']))
{
    $db = new Database();
    if($_POST['action']=="viewDealerPoints")
    {
        $ID = $_POST['ID'];
        $getPoints = $db->query("SELECT * FROM tbdealer WHERE dealerCode='$ID' and expiryDate >= CURDATE()","array");
        foreach($getPoints as $data)
        {
            $dealerPoints = $data['pointsEarned'];
            foreach (extractForms($dealerPoints) as $key => $value) {
                if(isset($total[$key]))
                    $total[$key] += $value; 
                else
                    $total[$key] = $value;
            }
        }
        
?>
<?php
if(@$_POST['secAction']=="lessButt"){
?>
    <div style="float: left; width: 97.5%; margin-left: 20px; position: relative;" id="viewPoints">
<?php
}else{
    ?>
    <div style="float: left; width: 90%; margin-left: 100px; position: relative;" id="viewPoints">
    <?php
}
?>
        <?php
            if(@$_POST['secAction']=="lessButt"){
                ?>
                <div class="fl_right align_right">
                    <input type="button" id="hideDPL" class="reports hide" value="Hide" title="Hide Dealer Points By Category">
                </div>
            <?php
            }
            ?>
        <?php
            if(empty($data['firstName'])&&empty($data['lastName'])){
                ?>
                <label class="h3 align_center">
                    No Record Found
                </label>
                <?php
            }else{
        ?>
            <label class="dealerName h3 <?php if(@$_POST['secAction']=="lessButt"){echo "display";} ?>" >
                Name: <?php echo $data['firstName']. " " . $data['lastName'];?>
            </label>
            <?php
            if(@$_POST['secAction']=="lessButt"){ //this is for report purposes
                    ?><div style="margin: 15px;" class="h4"><b>Dealer Points By Category</b></div><?php
                }else{
                }
            ?>
            <div style="width: 80%;margin-top: 10px; line-height: 20px; margin-left: 15px;">
                <div style="width: 100%;margin-left: 50px;" class="h2">
                    <table border="0" style="width: 100%;">
                        <?php
                        foreach ($total as $k => $v)  {
                            
                            $split = explode(" ", $k);
                            $header = $split[0]; // Header
                            
                            if(strpos($k, 'PLINE') !== false){
                                $remove_zero = str_replace("0","", $split[1]);
                                $k = $remove_zero; // Sub Header
                            }else{
                                $k = $k; // Sub Header
                            }
                            
                            $points = redeemPoints($k,$v,$ID);
                            if($points==0){
                            }else{
                                
                        ?>
                        <tr>
                            <td width="200">
                            <?php
                            $getProductLines = $db->query("SELECT * FROM tbproduct_line WHERE code='$k' AND is_active='1'","array");
                            foreach($getProductLines as $productLine){
                                $code = $productLine['code'];
                                $description = $productLine['description'];
                            }
                            if($code==$k){
                                echo $description;
                                //echo $k;
                            }else{
                                echo $k;
                            }
                            
                            ?>
                            </td>
                            <td width="200">
                                <?php
                                
                                echo $points. " " . "Points";
                                ?>
                            </td>
                            <td width="50">
                                <?php
                                if(@$_POST['secAction']=="lessButt"){ //this is for report purposes
                                    
                                }else{
                                ?>
                                <label id="<?php echo $points; ?>" data-header='<?php echo $header;?>' rel='<?php echo $k;?>' dealerCode="<?php echo $ID; ?>" class="cursor get_products">
                                    <input type="button" class="button" value="Redeem"/>
                                </label>
                                <?php
                                }
                                ?>
                            </td>
                            <td width="50">
                                <?php
                                if(@$_POST['secAction']=="lessButt"){ //this is for report purposes
                                    
                                }else{
                                ?>
                                <label id="<?php echo $points; ?>" data-header='<?php echo $header;?>' rel='<?php echo $k;?>' dealerCode="<?php echo $ID; ?>" class="cursor view_products">
                                    <input type="button" class="button" value="View"/>
                                </label>
                                <?php
                                }
                                ?>
                            </td>
                        </tr>
                        <?php
                            }
                        }
                        ?>
                    </table>
                </div>
            </div>
            <?php
            if(@$_POST['secAction']=="lessButt"){
            ?>
                <div style="width: 500px;position: absolute; top: 100px;" id="loadProducts" class="display">
            <?php
            }else{
                ?>
                <div style="width: 730px;position: absolute; top: 250px;" id="loadProducts" class="display">
                <?php
            }
            ?>
                <center>
                    <img src="/images/loader/loading.gif"/>
                </center>
            </div>
    </div>    
                
    
<?php
    }
    }
}
if(isset($_POST['generateTrans_id'])){
        $charset = "0123456789";
        $generate_transactionID = substr(str_shuffle($charset),0,5);
        $ID = $_POST['ID'];
        $transactionID = $ID."_".$generate_transactionID.time();
        echo $transactionID;
}
